#Andrew Gooch
#April 11, 2017
#Ripping Yarn, Political Communications
#Replication R file 4: Figure 4 favorability plot

#This R file creates Figure 4 using ggplot. I skip over Figure 3 in the replication order because Fig4 uses the same data as Figure 2

#REQUIREMENTS:
#Must run March_weights.R before running code below


#AVERAGE TREATMENT EFFECTS# REFERENCED IN TEXT
story_sub<- subset(Mar15.svy.rake, StoryDummy==1)
cont_sub<- subset(Mar15.svy.rake, ControlDummy==1)

svymean(~BidenFavor,design=cont_sub, na.rm=TRUE)
confint(svymean(~BidenFavor,design=cont_sub, na.rm=TRUE))

svymean(~BidenFavor,design=story_sub, na.rm=TRUE)
confint(svymean(~BidenFavor,design=story_sub, na.rm=TRUE))




# by party ID 


##########################################
# mean support and confidence intervals MARCH
##########################################

#Personalized Story treatment

#Dems
storysub_Dem <- subset(Mar15.svy.rake, StoryDummy==1 & PID3_lean_new==1)

Story_mean_Mar15_Dem <- svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE)[1]
Story_lower_Mar15_Dem <- confint(svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE))[1]
Story_upper_Mar15_Dem <- confint(svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE))[2]

#Reps
storysub_Rep <- subset(Mar15.svy.rake, StoryDummy==1 & PID3_lean_new==-1)

Story_mean_Mar15_Rep <- svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE)[1]
Story_lower_Mar15_Rep <- confint(svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE))[1]
Story_upper_Mar15_Rep <- confint(svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE))[2]

#Inds
storysub_Ind <- subset(Mar15.svy.rake, StoryDummy==1 & PID3_lean_new==0)

Story_mean_Mar15_Ind <- svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE)[1]
Story_lower_Mar15_Ind <- confint(svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE))[1]
Story_upper_Mar15_Ind <- confint(svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE))[2]

#Control Group

#Dems
contrsub_Dem <- subset(Mar15.svy.rake, ControlDummy==1 & PID3_lean_new==1)

contr_mean_Mar15_Dem <- svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE)[1]
contr_lower_Mar15_Dem <- confint(svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE))[1]
contr_upper_Mar15_Dem <- confint(svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE))[2]

#Reps
contrsub_Rep <- subset(Mar15.svy.rake, ControlDummy==1 & PID3_lean_new==-1)

contr_mean_Mar15_Rep <- svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE)[1]
contr_lower_Mar15_Rep <- confint(svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE))[1]
contr_upper_Mar15_Rep <- confint(svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE))[2]

#Inds
contrsub_Ind <- subset(Mar15.svy.rake, ControlDummy==1 & PID3_lean_new==0)

contr_mean_Mar15_Ind <- svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE)[1]
contr_lower_Mar15_Ind <- confint(svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE))[1]
contr_upper_Mar15_Ind <- confint(svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE))[2]



##########################################
# 3-panel plot, one for each PID, MARCH
##########################################

#first panel plot - Democrats

coef <- c(contr_mean_Mar15_Dem,
          Story_mean_Mar15_Dem)

lb <-c(contr_lower_Mar15_Dem,
       Story_lower_Mar15_Dem)

ub <-c(contr_upper_Mar15_Dem,
       Story_upper_Mar15_Dem)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p1 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("blue","blue")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Democrats") +
  scale_y_continuous("Favorability of Vice President Joe Biden (0-1)", limits=c(0.2,.8)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p1)

#second panel plot - Independents

coef <- c(contr_mean_Mar15_Ind,
          Story_mean_Mar15_Ind)

lb <-c(contr_lower_Mar15_Ind,
       Story_lower_Mar15_Ind)

ub <-c(contr_upper_Mar15_Ind,
       Story_upper_Mar15_Ind)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p2 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("grey","grey")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Independents") +
  scale_y_continuous(" ", limits=c(0.2,.8)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p2)


#second panel plot - anonymous stories

coef <- c(contr_mean_Mar15_Rep,
          Story_mean_Mar15_Rep)

lb <-c(contr_lower_Mar15_Rep,
       Story_lower_Mar15_Rep)

ub <-c(contr_upper_Mar15_Rep,
       Story_upper_Mar15_Rep)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p3 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("red","red")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Republicans") +
  scale_y_continuous(" ", limits=c(.2,.80)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p3)

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p2)

p4 <- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                               p2 + theme(legend.position="none"),
                               p3 + theme(legend.position="none"),
                               nrow=1),
                   mylegend, nrow=2,heights=c(10, 1))




